﻿

using System;
using System.Data;
using System.Data.SqlClient;
using Model;
namespace Dal
{
	public class ServerIPDal
	{
		public ServerIPDal()
		{
		}
 		#region Insert
 		public static int Insert(ServerIP serverIP)
		{	
			string sql = @"Insert Into ServerIP (
					IP,
					LastTime,
					StateR
				) values('{0}','{1}',{2})";
			sql = string.Format(sql,
					SqlHelper.String(serverIP.IP),
					serverIP.LastTime,
					serverIP.StateR
				);
			return SqlHelper.ExecSql(sql);
		}
 		public static int InsertRefIdentity(ServerIP serverIP)
		{	
			string sql = @"Insert Into ServerIP (
					IP,
					LastTime,
					StateR
				) values('{0}','{1}',{2})";
			sql = string.Format(sql,
					SqlHelper.String(serverIP.IP),
					serverIP.LastTime,
					serverIP.StateR
				);
			int iResult = 0;
			SqlHelper.ExecSql(sql,ref iResult);
			return iResult;
		}		
		public static int InsertByProc(ServerIP serverIP,ref string strResult)
		{
			IDataParameter[] parameters = new SqlParameter[4];

			parameters[0]=new SqlParameter("@ID", SqlDbType.Int);
    		parameters[3]=new SqlParameter("@StateR", SqlDbType.Int);	

			parameters[0].Value = ParameterDirection.Output;
			parameters[3].Value = serverIP.StateR;

			
			SqlHelper.RunProcedure("serverIP_Insert", parameters, "PROC");
			int iResult = Convert.ToInt32(parameters[0].Value);
			return iResult;
		}
		#endregion
		
		#region Update
 		public static int Update(ServerIP serverIP)
		{	
			string sql = @"Update ServerIP set 

					IP= '{0}',
					LastTime= '{1}',
					StateR= {2}
				where

					ID= {3}";
			sql = string.Format(sql,
					SqlHelper.String(serverIP.IP),
					serverIP.LastTime,
					serverIP.StateR,
					serverIP.ID);
			return SqlHelper.ExecSql(sql);
		}
		public static int UpdateByProc(ServerIP serverIP,ref string strResult) 
		{
			IDataParameter[] parameters = new SqlParameter[4];
            
	       	parameters[0]=new SqlParameter("@ID", SqlDbType.Int);
	       	parameters[3]=new SqlParameter("@StateR", SqlDbType.Int);             
            
			parameters[0].Value = serverIP.ID;
			parameters[3].Value = serverIP.StateR;
   
	            
	        int iOutRowAffected=0;
	        int iResult = SqlHelper.RunProcedure("serverIP_Update", parameters, out iOutRowAffected);
	        return iResult;
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 iD)
		{
		
			string sql = @"Delete from ServerIP where 
					ID= {0}";
			sql = string.Format(sql,
			iD
);
			return SqlHelper.ExecSql(sql);
		} 
		
		public static int DeleteList(string FieldsName,string Values)
		{
			string sql = @"Delete from ServerIP where "+FieldsName+" in ("+Values+")";
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from ServerIP a "; 
			return SqlHelper.Select(sql);
		}
				
		public static ServerIP Select(Int32 iD)
		{
			ServerIP serverIP = null;
			string sql = @"select a.* from ServerIP a  where
					a.ID= {0}
";
			sql = string.Format(sql,
			iD
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				serverIP = new ServerIP(ds.Tables[0].Rows[0]);					
			return serverIP;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from ServerIP a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from ServerIP a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from ServerIP a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
